package org.hackystat.devcathlon.ui.page.matches;

import java.io.Serializable;
import java.util.Comparator;
import org.hackystat.devcathlon.engine.event.EventScore;

/**
 * Comparator class used to sort Event Scores by date.
 * 
 * @author John Zhou
 */
public class EventScoreDateComparator implements Comparator<Object>, Serializable {
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  /** Boolean for ascending or descending sort. */
  private boolean ascending;
  
  /**
   * Instantiates a EventScoreDateComparator.
   * 
   * @param ascending The boolean for ascending or descending sort.
   */
  public EventScoreDateComparator(boolean ascending) {
    this.ascending = ascending;
  }
  
  /**
   * Override compare so that Event Scores can be sorted by date.
   * 
   * @param obj1 The first EventScore to be sorted.
   * @param obj2 The second EventScore to be sorted.
   * @return compare A negative, zero, or a positive integer corresponding to less than, equal to,
   * or greater than.
   */
  public int compare(Object obj1, Object obj2) {
    EventScore a = (EventScore) obj1;
    EventScore b = (EventScore) obj2;
    int compare = 0;
    try {
      compare = b.getTimestampDate().compare(a.getTimestampDate());
    }
    catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    if (!ascending) {
      compare *= -1;
    }
    return compare;
  }
}
